//
//  YYSRegisterController.m
//  FanLaiWu
//
//  Created by gonglei on 17/3/17.
//  Copyright © 2017年 yys. All rights reserved.
//

#import "YYSRegisterController.h"

@interface YYSRegisterController ()
@property (weak, nonatomic) IBOutlet UITextField *passwoldText;
@property (weak, nonatomic) IBOutlet UITextField *passworldagintext;
@property (weak, nonatomic) IBOutlet UITextField *codeText;
@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;

@property (weak, nonatomic) IBOutlet UITextField *phoneText;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;
@end

@implementation YYSRegisterController
 NSTimer *get_timer;
int time_index;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.titleLabel.text = self.title;
    
    
    self.sureBtn.layer.masksToBounds = YES;
    self.sureBtn.layer.cornerRadius = 5;
}
- (IBAction)backClick:(id)sender {
    if (get_timer) {
        [get_timer invalidate];
        get_timer = nil;
    }
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];

}
- (IBAction)clickGetCode:(UIButton *)sender {
    if (_phoneText.text.length != 11) {
        
        [SVProgressHUD setMinimumDismissTimeInterval:1];
        [SVProgressHUD showErrorWithStatus:@"请输入正确手机号"];
        return;
    }
    
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"mobile"] = _phoneText.text;
    if ([self.title isEqualToString:@"密码重置"]) {
        dict[@"type"] = @"reset";
    } else {
        
        dict[@"type"] = @"signup";
    }
    [NSObject POST:SendCode_URL parameters:dict progress:^(NSProgress *downloadProgress) {
        
    } completionHandler:^(id responseObj, NSError *error) {
//        NSLog(@"%@",responseObj);
        if ([responseObj[@"errno"] integerValue] == 0) {
            _codeText.text = [responseObj[@"result"][@"code"] stringValue];
            [sender setEnabled:NO];
            
            time_index = 60;
            [sender setTitle:[NSString stringWithFormat:@"%ds后重新发送",time_index] forState:UIControlStateNormal];
            
            get_timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
            [get_timer fire];
        } else {
            [SVProgressHUD showErrorWithStatus:responseObj[@"errmsg"]];
        }
    }];
}
- (void)timerAction {
    time_index = time_index-1;
    
    if (time_index <= 0) {
        time_index = 0;
        [get_timer invalidate];
        get_timer = nil;
        
        [_getCodeBtn setEnabled:YES];
        
        
        [_getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_getCodeBtn setBackgroundColor:[UIColor whiteColor]];
    }
    else {
        [_getCodeBtn
         setTitle:[NSString stringWithFormat:@"%ds后重新发送",time_index] forState:UIControlStateNormal];
        [_getCodeBtn setBackgroundColor:YYSRGBColor(220, 220, 220)];
    }
}
- (IBAction)sureClick:(id)sender {
    if (_phoneText.text.length != 11) {
        
        [SVProgressHUD setMinimumDismissTimeInterval:1];
        [SVProgressHUD showErrorWithStatus:@"请输入正确手机号"];
        return;
    }
    if (_passwoldText.text.length <= 5) {
        
        [SVProgressHUD setMinimumDismissTimeInterval:1];
        [SVProgressHUD showErrorWithStatus:@"请输入密码(6~16)"];
        return;
    }
    if (![_passwoldText.text isEqualToString:_passworldagintext.text]) {
        
        [SVProgressHUD setMinimumDismissTimeInterval:1];
        [SVProgressHUD showErrorWithStatus:@"两次密码不一致"];
        return;
    }
    if (_codeText.text.length <= 0) {
        
        [SVProgressHUD setMinimumDismissTimeInterval:1];
        [SVProgressHUD showErrorWithStatus:@"请输入验证码"];
        return;
    }
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"mobile"] = _phoneText.text;
    dict[@"password"] = _passwoldText.text;
    dict[@"repassword"] = _passworldagintext.text;
    dict[@"captcha"] = _codeText.text;
    [NSObject POST:Register_URL parameters:dict progress:^(NSProgress *downloadProgress) {
        
    } completionHandler:^(id responseObj, NSError *error) {
//        NSLog(@"%@",responseObj);
        if ([responseObj[@"errno"] integerValue] == 0) {
            [SVProgressHUD setMaximumDismissTimeInterval:2];
            [SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"%@成功",self.title]];
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        } else {
            [SVProgressHUD showErrorWithStatus:responseObj[@"errmsg"]];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
